// CustomVariable.cpp: implementation of the CCustomVariable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "CustomVariable.h"
#include "PropertyOwner.h"
#include "CustomDBVariable.h"
#include "CustomNonDBVariable.h"
#include "PropertyExpression.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
namespace pa
{
	CustomVariable::CustomVariable()
	{
		m_pVar = NULL;
		m_nPrior = 0;
		m_bIsActive = 0;
	}

	CustomVariable::~CustomVariable()
	{

	}

	void CustomVariable::CopyCustomData(CustomVariable& Dis, const CustomVariable& Source)
	{
		Dis.m_strPrompt = Source.m_strPrompt;
		Dis.m_strPromptSource = Source.m_strPromptSource;
		Dis.m_nPrior = Source.m_nPrior;
		Dis.m_bIsActive = Source.m_bIsActive;
		for (int i = 0; i < Source.m_strInfluenceArr.GetSize(); i++)
		{
			Dis.m_strInfluenceArr.Add(Source.m_strInfluenceArr.GetAt(i));
		}
	}


	void CustomVariable::Influence(const CString& strInflunce)
	{
		m_strInfluenceArr.RemoveAll();

		TCHAR buf[256];
		INT nIndex = 0;

		for(int i = 0 ; i < strInflunce.GetLength(); i++)
		{
			TCHAR c = strInflunce.GetAt(i);

			if(c != _T(','))
			{
				buf[nIndex] = c;
				nIndex++;
			}
			else
			{
				buf[nIndex] = _T('\0');
				m_strInfluenceArr.Add(buf);
				nIndex = 0;
			}			
		}

		if(nIndex != 0)
		{
			buf[nIndex] = _T('\0');
			m_strInfluenceArr.Add(buf);
			nIndex = 0;
		}
	}

	void CustomVariable::Influence()
	{
		ASSERT(m_pVar);
		if(m_strInfluenceArr.GetSize() <= 0)
		{
			return;
		}

		for(int i = 0; i < m_strInfluenceArr.GetSize(); i++)
		{
			CString strTemp = m_strInfluenceArr.GetAt(i);
			ASSERT(!strTemp.IsEmpty());
			Property* pVar = m_pVar->GetOwner()->GetPropertyContainer()->GetProperty(strTemp);
			ASSERT(pVar);
			ASSERT(pVar->IsCustomization());

			CustomVariable* pCVar = NULL;
			if(pVar->PropertyStyle() == Property::eDB)
			{
				pCVar = static_cast<CustomDBVariable*>(pVar);
			}
			else
			{
				pCVar = static_cast<CustomNonDBVariable*>(pVar);
			}

			pCVar->UpdateData();
			pCVar->Influence();
		}
	}

	void CustomVariable::UpdateData()
	{
	}


	BOOL CustomVariable::IsCanInluence(Property* pVar)
	{
		ASSERT(pVar);
		if(m_strInfluenceArr.GetSize() <= 0)
		{
			return FALSE;
		}

		for(int i = 0; i < m_strInfluenceArr.GetSize(); i++)
		{
			CString strTemp = m_strInfluenceArr.GetAt(i);
			ASSERT(!strTemp.IsEmpty());
			Property* pTempVar = m_pVar->GetOwner()->GetPropertyContainer()->GetProperty(strTemp);
			ASSERT(pTempVar);

			if(pVar->GetSymbol() == pTempVar->GetSymbol())
			{
				return TRUE;
			}
		}
		
		return FALSE;
	}
};